package com.liao.utils;

import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.bean.result.Result;
import com.liao.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.text.DecimalFormat;

@Component
public class MyUtils {

    @Autowired
    private ModuleService moduleService;

    public Result  addModule (String parent_module,String title) throws Exception {
        String moduleId=moduleService.queryModuleByFinallyId(parent_module);
        //将菜单id自增加1
        int i = Integer.parseInt(moduleId);
        int k=i+1;
        String s = String.valueOf(k);

        int length = parent_module.length();
        StringBuilder builder = new StringBuilder();
        for (int j =0;j<length+2;i++){
            builder.append("0");
        }
        DecimalFormat df=new DecimalFormat(builder.toString());
        moduleId=df.format(Integer.parseInt(s));

        //根据菜单名称添加二级菜单
        //判断菜单名称是否已经存在

        boolean flag=moduleService.queryModuleByTitle(title,parent_module);
        if (!flag){
            moduleService.addFirstModule(moduleId,title, parent_module);
            return new Result(true, Code.MODULE_ADD_SUCCESS, Msg.MODULE_ADD_SUCCESS);
        }
        return new Result(false,Code.MODULE_ADD_FAIL,Msg.MODULE_ADD_FAIL);
    }
}
